﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Services;
using MvcApplication1.Models;

namespace MvcApplication1.Controllers
{
    public abstract class ApplicationController : Controller
    {
        /// <summary>
        /// get topics from database and map to Topic model
        /// </summary>
        /// <returns>List<Topic> topics</returns>
        public static List<Topic> getTopics()
        {
            var lTopic = new List<get_topicsResult>();
            try {
                using (vulnerability_dbDataContext context = new Services.vulnerability_dbDataContext())
                {
                    var result = context.get_topics();
                    lTopic = result.ToList();
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex);
            }

            // translate linq result to model Topics
            var topics = new List<Topic>();
            foreach (var item in lTopic)
            {
                var topic = new Topic();
                topic.Name = item.name;
                topic.Abbr = (item.abbr).Trim();
                //handle xml topic.Prerequisits = item.prerequisit;
                topics.Add(topic);
            }
            return topics;
        }

        /// <summary>
        /// pass topics as view data
        /// </summary>
        public ApplicationController()
        {
            var topics = new List<Topic>();
            topics = getTopics();
            ViewData["topics"] = topics;
        }

    }
}
